package com.tledu.controller;


import com.tledu.common.AjaxResult;
import com.tledu.model.User;
import com.tledu.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private IUserService userService;

    //转到登录页面
    @RequestMapping("/toLogin")
    public String toLogin(Model model) {
        return "login";
    }

    //登录页面
    @RequestMapping("/login")
    @ResponseBody
    public AjaxResult login(User user, Model model) {
        User u = userService.login(user);
        AjaxResult result = new AjaxResult();
        Map<String, Object> map = new HashMap<String, Object>();
        if (u != null) {
            model.addAttribute("username", user.getUsername());
            map.put("userId", u.getUserId());
            map.put("roleId", u.getRoleId());
            result.setData(map);
            result.setSuccess(true);
        } else {
            result.setSuccess(false);
        }
        return result;
    }

    //主页展示页面
    @RequestMapping("/index")
    public String index(Model model, String username, String roleId, Integer userId) {
        if (username != null && !username.equals("")) {
            model.addAttribute("userId", userId);
            model.addAttribute("username", username);
            model.addAttribute("roleId", roleId);
        } else {
            model.addAttribute("roleId", 3);
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("noLogin", true);
        return "index";
    }

    //到注册页面
    @RequestMapping("/toRegister")
    public String toRegister(Model model) {
        return "register";
    }

    //注册
    @RequestMapping("/register")
    @ResponseBody
    public AjaxResult register(User user, Model model) {
        List<User> list = userService.selectByUserName(user.getUsername());
        AjaxResult result = new AjaxResult();
        if (list.size() == 0) {
            user.setRoleId(3);
            userService.register(user);
            result.setSuccess(true);
        } else {
            result.setSuccess(false);
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("noLogin", true);
        return result;
    }

    //退出登录
    @RequestMapping("/exit")
    public String exit(Model model) {
        model.addAttribute("isLogin", null);
        model.addAttribute("noLogin", null);
        model.addAttribute("roleId", 3);
        return "index";
    }

    @RequestMapping("home")
    public String home() {
        return "index";
    }

    @RequestMapping("account")
    public String account(User user, Model model, String userId) {
        User user1 = userService.selectByUserId(userId);
        model.addAttribute("user1", user1);
        model.addAttribute("isLogin", true);
        model.addAttribute("noLogin", true);
        return "account";
    }

    @RequestMapping("update")
    @ResponseBody
    public AjaxResult update(User user,Model model) {
        AjaxResult result = new AjaxResult();
        try {
            userService.update(user);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("noLogin", true);
        return result;
    }

    @RequestMapping("toAddress")
    public String toAddress(Model model ,Integer userId) {
        model.addAttribute("userId", userId);
        model.addAttribute("isLogin", true);
        model.addAttribute("noLogin", true);
        return "address";
    }
    @RequestMapping("addressAdd")
    @ResponseBody
    public AjaxResult addressAdd(User user,Model model,String userId){
        AjaxResult result = new AjaxResult();
//        User user1 = userService.selectByUserId(userId);
//        model.addAttribute("user1", user1);
        try {
            userService.addressAdd(user);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }
        return result;
    }

//    @RequestMapping("center")
//    public String center(Model model ,String userId){
//
////        model.addAttribute("userId", userId);
//        return "account";
//    }
}
